﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseLibrary.CDataContext;
namespace DataSyn.DAL.DB
{
    public class DataContext: BaseLibrary.CDataContext.DataContext
    {
        public DataContext(EProvider provider, string connectionString):base(provider,connectionString)
        {
        }
        public CTable<Tables.TConnection> TConnection = new CTable<Tables.TConnection>();
        public CTable<Tables.TProject> TProject = new CTable<Tables.TProject>();
        public CTable<Tables.TDataBase> TDataBase = new CTable<Tables.TDataBase>();
        public CTable<Tables.TDataTable> TDataTable = new CTable<Tables.TDataTable>();
        public CTable<Tables.TSyncProcess> TSyncProcess = new CTable<Tables.TSyncProcess>();
        #region 对同步的库连接的控制
        Dictionary<int, BaseLibrary.CDataContext.DataContext> dic_SyncDataBase_DC = new Dictionary<int, BaseLibrary.CDataContext. DataContext>();
        public BaseLibrary.CDataContext.DataContext GetDataBaseDC(int DataBaseID)
        {
            BaseLibrary.CDataContext.DataContext back = null;
            lock (dic_SyncDataBase_DC)
            {
                if (dic_SyncDataBase_DC.ContainsKey(DataBaseID))
                {
                    back = dic_SyncDataBase_DC[DataBaseID];
                }
                else
                {
                    try
                    {
                        var database = TDataBase.FirstOrDefault(c => c.ID == DataBaseID);
                        if (database == null) throw new Exception("");
                        var conn = TConnection.FirstOrDefault(c => c.ID == database.ConnectionID);
                        if (conn == null) throw new Exception("");
                        var tem = new BaseLibrary.CDataContext.ConnectionSetting.Conn() { Provider = conn.Provider, Connection = conn.ConnectionString };
                        tem.DataName = database.DataBase;
                        var newDC = new BaseLibrary.CDataContext.DataContext(tem.Provider, tem.Connection);
                        newDC.MaxConnCount = 4;
                        dic_SyncDataBase_DC.Add(DataBaseID, newDC);
                        back = newDC;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            if (back == null)
                throw new Exception("");
            return back;
        }
        /// <summary>
        /// 得到主数据源
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public Tables.TDataBase GetMasterBase(int projectID)
        {
            return TDataBase.FirstOrDefault(c => c.ProjectID == projectID && c.IsMasterDB);
        }
        /// <summary>
        /// 得到主连接
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public BaseLibrary.CDataContext.DataContext GetMasterDC(int projectID)
        {
            var masterBase = GetMasterBase(projectID);
            if (masterBase == null) return null;
            return GetDataBaseDC(masterBase.ID);
        }
        /// <summary>
        /// 得到备数据源
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public List<Tables.TDataBase> GetClientBase(int projectID)
        {
            return TDataBase.Where(c => c.ProjectID == projectID).ToList().Where(c => !c.IsMasterDB && !string.IsNullOrWhiteSpace(c.DataBase)).ToList();
        }
        /// <summary>
        /// 得到备连接
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public List<BaseLibrary.CDataContext.DataContext> GetClientDC(int projectID)
        {
            return GetClientBase(projectID).Select(c => GetDataBaseDC(c.ID)).ToList();
        }
        public List<BaseLibrary.CDataContext.DataContext> GetAllDC(int projectID)
        {
            List<BaseLibrary.CDataContext.DataContext> list_back = new List<BaseLibrary.CDataContext.DataContext>();
            list_back.Add(GetMasterDC(projectID));
            list_back.AddRange(GetClientDC(projectID));
            return list_back;
        }
        #endregion

    }
}
